package com.goldman.aplt.util;

import com.fasterxml.jackson.databind.JsonNode;
import com.goldman.aplt.base.constant.ApConstants;
import com.goldman.aplt.base.core.CoreRespone;
import com.goldman.aplt.base.eb.EbResponse;
import com.goldman.aplt.base.exception.BankBusiException;
import com.goldman.aplt.base.interceptor.ApltExceptionInterceptor;
import com.goldman.aplt.base.type.CmErrorCode;
import com.goldman.aplt.entity.AppErrorMapping;
import com.goldman.aplt.service.AppErrorMappingService;
import com.goldman.aplt.service.packet.ApsOutLogService;
import com.goldman.aplt.service.parameter.AppSystemParameterService;
import com.goldman.http.common.s2s.S2SHttp;
import com.goldman.http.common.s2s.S2SOption;
import com.goldman.http.common.s2s.S2SResponse;
import com.goldman.http.common.s2s.model.ResponseData;
import com.goldman.http.common.s2s.util.JsonUtil;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * 统一的外调请求工具
 */
@Component
@Slf4j
public class EBUtil {

    @Autowired
    @Qualifier("apsOutLogService")
    private ApsOutLogService apsOutLogServiceImpl;
    private static ApsOutLogService apsOutLogService;

    @Autowired
    private AppErrorMappingService errorMappingService;

    @PostConstruct
    public void init() {
        apsOutLogService = apsOutLogServiceImpl;
    }

    public EbResponse post(String url, Map<String, Object> req) {
        S2SResponse<Object> response = HttpUtil.post(url, JsonUtil.toJsonStr(req),
                S2SOption.builder().contentType(S2SOption.ContentType.JSON).timeout(35 * 1000).build());

        if (!response.isHTTPSuccess()) {
            return errorProcess(true, response.getHttpStatus() + "", "http error", response.getHttpStatus() == -1, true);
        }

        if (!response.getResponseData().isSuccess()) {
            return errorProcess(true, response.getResponseData().getStatus().getCode() + "", response.getResponseData().getStatus().getMsg(), false, true);
        }

        ResponseData<Object> responseData = response.getResponseData();

        if (responseData.getStatus().getCode() != 0) {
            return errorProcess(true, String.valueOf(responseData.getStatus().getCode()), String.valueOf(responseData.getStatus().getMsg()), false, false);
        }
        JsonNode data = JsonUtil.toTree(JsonUtil.toJsonStr(response.getResponseData().getData()));
        return EbResponse.builder().data(data).isSuccess(false).isTimeOut(false).isSystemError(false).build();
    }

    private EbResponse errorProcess(boolean throwError, String errCode, String errMsg, boolean isTimeOut, boolean isSystemError) {

        String retmsg = ApltExceptionInterceptor.DEFAULT_ERROR_MSG.replace("ERRORCODE", "[" + CmErrorCode.EB_SYS_ERROR.getCode() + "]" + errCode);
        if (throwError) {
            AppErrorMapping appErrorMapping = errorMappingService.selOne(errCode, "en-US");
            if (appErrorMapping != null) {
                retmsg = appErrorMapping.getMessage();
            }

            throw new BankBusiException(CmErrorCode.EB_SYS_ERROR, "eb系统异常！errMsg:" + errMsg, retmsg);

        } else {
            return EbResponse.builder().errorCode(errCode)
                    .errorMsg(errMsg)
                    .isSuccess(false).isTimeOut(isTimeOut).isSystemError(isSystemError).build();
        }
    }
}
